const proto_man = require("proto_man");
const proto_tools = require("proto_tools");
/** 
 Enter
 客户端 进入聊天室
 1, 1, body = {
     uname: "名字",
     usex: 0 or 1 //性别
 }
 返回
 1, 1, status = OK
*/

/** 
 Exit
 客户端 离开聊天室
 1, 2, body = null;
 返回
 1, 2, status = OK
*/

/** 
 UserEnter: 主动发送
 1, 3, body = uinfo:{
     uname: "名字",
     usex: 0 or 1 //性别
 };

 UserExit: 主动离开
 1, 4, body = uinfo{
     uname: "名字",
     usex: 0, 1 //性别
 }

 SendMsg:
 1, 5, body = `消息内容`
 返回
 1, 5, body = {
     0: status, OK
     1: uname,
     2: usex,
     3: msg, // 消息内容
 }

 UserMsg:
 1, 6, body = {
     0: uname,
     1: usex,
     2: msg,
 };
*/

let STYPE_TALKROOM = 1;
let TalkCmd = {
    Enter: 1, //用户进来
    Exit: 2, //用户离开
    UserArrived: 3, //别人进来
    UserExit: 4, //别人离开
    SendMsg: 5, //自己发送消息
    UserMsg: 6  //别人发送消息
}

const Response = {
    OK: 1,
    IS_IN_TALKROOM: -100,       //玩家已经在聊天室
    NOT_IN_TALKROOM: -101,      //玩家不在聊天室
    INVALID_OPT: -102,          //玩家非法操作
    INVALID_PARAMS: -103,       //玩家参数不正确
}

/** 二进制编码解码 */
function decode_user_enter(cmd_buf) {
    let cmd = {};
    cmd[0] = proto_tools.read_int16(cmd_buf, 0);
    cmd[1] = proto_tools.read_int16(cmd_buf, 2);
    let offset = proto_tools.header_size;

    let body = {};
    cmd[2] = body;

    let ret = proto_tools.read_str_inbuf(cmd_buf, offset);
    body.uname = ret[0];
    offset = ret[1];

    body.usex = proto_tools.read_int16(cmd_buf, offset);
    offset += 2;
    
    return cmd;
}
proto_man.reg_buf_decoder(1, 3, decode_user_enter);

function decode_user_exit(cmd_buf) {
    let cmd = {};
    cmd[0] = proto_tools.read_int16(cmd_buf, 0);
    cmd[1] = proto_tools.read_int16(cmd_buf, 2);

    let offset = proto_tools.header_size;
    let body = {};
    cmd[2] = body;

    let ret = proto_tools.read_str_inbuf(cmd_buf, offset);
    body.uname = ret[0];
    offset = ret[1];

    body.usex = proto_tools.read_int16(cmd_buf, offset);
    offset += 2;
    return cmd;
}
proto_man.reg_buf_decoder(1, 4, decode_user_exit);

function encode_enter_talkroom(stype, ctype, body) {
    let uname_len = body.uname.utf8_byte_len();
    let total_len = proto_tools.header_size + 2 + uname_len + 2;

    let cmd_buf = proto_tools.alloc_buffer(total_len);
    let offset = proto_tools.write_cmd_header_inbuf(cmd_buf, stype, ctype);
    offset = proto_tools.write_str_inbuf(cmd_buf, offset, body.uname, uname_len);

    proto_tools.write_int16(cmd_buf, offset, body.usex);
    return cmd_buf;
}
proto_man.reg_buf_encoder(1, 1, encode_enter_talkroom);
proto_man.reg_buf_decoder(1, 1, proto_tools.decode_status_cmd);

proto_man.reg_buf_encoder(1, 2, proto_tools.encoded_empty_cmd);
proto_man.reg_buf_decoder(1, 2, proto_tools.decode_status_cmd);

function decode_send_msg(cmd_buf) {
    cc.log(`decode_send_msg`);
    let cmd = {};
    cmd[0] = proto_tools.read_int16(cmd_buf, 0);
    cmd[1] = proto_tools.read_int16(cmd_buf, 2);

    let offset = proto_tools.header_size;

    let body = {};
    let status = proto_tools.read_int16(cmd_buf, offset);
    offset += 2;

    body[0] = status;
    cmd[2] = body;
    if (status != Response.OK) {
        return cmd;
    }

    /** uname */
    let ret = proto_tools.read_str_inbuf(cmd_buf, offset);
    body[1] = ret[0];
    offset = ret[1];

    /** usex */
    body[2] = proto_tools.read_int16(cmd_buf, offset);
    offset += 2;

    /** msg */
    ret = proto_tools.read_str_inbuf(cmd_buf, offset);
    body[3] = ret[0];

    return cmd;
}
proto_man.reg_buf_encoder(1, 5, proto_tools.encode_str_cmd);
proto_man.reg_buf_decoder(1, 5, decode_send_msg);

function decode_user_msg(cmd_buf) {
    let cmd = {};
    cmd[0] = proto_tools.read_int16(cmd_buf, 0);
    cmd[1] = proto_tools.read_int16(cmd_buf, 2);
    let offset = proto_tools.header_size;

    let body = {};
    cmd[2] = body;

    /** uname */
    let ret = proto_tools.read_str_inbuf(cmd_buf, offset);
    body[0] = ret[0];
    offset = ret[1];

    /** usex */
    body[1] = proto_tools.read_int16(cmd_buf, offset);
    offset += 2;

    /** msg */
    ret = proto_tools.read_str_inbuf(cmd_buf, offset);
    body[2] = ret[0];
    return cmd;
}
proto_man.reg_buf_decoder(1, 6, decode_user_msg);

module.exports = {
    stype: STYPE_TALKROOM,
    cmd: TalkCmd,
    response: Response,
}